package com.example.blmap;

import android.content.Context;
import android.database.DataSetObserver;
import android.database.Observable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.locat.abs.Point;

import java.text.DecimalFormat;
import java.util.List;

public class BlMapView extends View {

    private Context context;
    private Paint mBeaconPaint = null;
    private Paint mRevcPaint = null;
    private Paint mAxisPaint = null;
    private Paint mTextPaint = null;
    private BlMapAdapter adapter;

    private BlMapAdapterObserver observer = new BlMapAdapterObserver();

    private Observable<BlMapAdapterObserver> mObserver;

    public BlMapView(Context context) {
        this(context,null);
    }

    public BlMapView(Context context,  AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BlMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);

    }

    public BlMapView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;

        mBeaconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBeaconPaint.setColor(this.getResources().getColor(R.color.colorForBeacon));
        mBeaconPaint.setAlpha(50);
        mBeaconPaint.setStrokeWidth(12);

        mRevcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRevcPaint.setColor(this.getResources().getColor(R.color.colorForUser));
        mRevcPaint.setAlpha(50);
        mRevcPaint.setStrokeWidth(12);

        mAxisPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAxisPaint.setColor(this.getResources().getColor(R.color.colorAxis));
        mAxisPaint.setStrokeWidth(2);

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(25);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Circle circle = adapter.getBeaconPoisition(context,this);
        drawAxis(canvas);
        drawBeacon(canvas);
        drawRevicer(canvas);
    }

    private void drawAxis(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        float rate = height / width;
        float spec = width / 30;
        for(int i=0;i<=height;i+=spec){
                canvas.drawLine(0,i,width,i,mAxisPaint);

        }
        for(int j=1;j<=width;j+=spec){
            canvas.drawLine(j,0,j,height,mAxisPaint);
        }

        Log.d("BlMapView",getWidth()+", "+getHeight());
    }

    private void drawRevicer(Canvas canvas) {
        List<Revicer> revicers = adapter.getRevicers();
        if(revicers != null && !revicers.isEmpty()){
            for(Revicer revicer : revicers){
                Point location = revicer.getLocation();
                canvas.drawCircle(location.getX(),location.getY(),revicer.getRadius(),mRevcPaint);
                canvas.drawText(location.getX()+","+location.getY()+"",location.getX(),location.getY(),mTextPaint);
            }
        }
    }

    private void drawBeacon(Canvas canvas) {
        List<Beacon> beacons = adapter.getBeacons();
        if(beacons != null && !beacons.isEmpty()){
            for(Beacon beac : beacons){
                Point location = beac.getLocation();
                canvas.drawCircle(location.getX(),location.getY(),beac.getLength(),mBeaconPaint);
                DecimalFormat df = new DecimalFormat("#00.00");
                canvas.drawText("["+beac.getMacAddress().substring(15,17)+"] : "+beac.getLength()/225f+"m",location.getX(),location.getY(),mTextPaint);
                //canvas.drawText(beac.getUuid(),location.getX(),location.getY(),mTextPaint);
              //  Animation anim = new TranslateAnimation()
            }
        }

    }

    public void setAdapter(BlMapAdapter adapter){
        this.adapter = adapter;
        adapter.registerDataSetObserver(observer);
    }



    class BlMapAdapterObserver extends DataSetObserver{
        @Override
        public void onChanged() {
            super.onChanged();
            Log.d("BlMapView","onchanged");
            invalidate();
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
            Log.d("BlMapView","onInvalidated");
        }
    }
}
